package com.xiaonei.storm.adapter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xiaonei.storm.ObserverPrx;
import com.xiaonei.storm.ObserverPrxHelper;
import com.xiaonei.storm._ObserverDisp;

public abstract class IceStormListener extends _ObserverDisp {

    private ObserverPrx proxy;

    private Log logger = LogFactory.getLog(IceStormListener.class);

    public abstract void handle(String message);

    public ObserverPrx register(Ice.ObjectAdapter adapter) {
        proxy = ObserverPrxHelper.uncheckedCast(adapter.addWithUUID(this).ice_timeout(300));
        return proxy;
    }

    public ObserverPrx deregister(Ice.ObjectAdapter adapter) {
        adapter.remove(proxy.ice_getIdentity());
        return proxy;
    }

    public ObserverPrx getProxy() {
        return proxy;
    }

    public void update(String message, Ice.Current current) {
        try {
            IceStormAdapter.getInstance().getThreadPoolExecutor().execute(
                    new IceStormTask(this, message));
        } catch (Exception e) {
            logger.error("跟新！", e);
        }
    }

    /* (非 Javadoc)
     * <p>Title: toString</p>
     * <p>Description: </p>
     * @return
     * @see java.lang.Object#toString()
    */
    @Override
    public String toString() {
        return "IceStormListener [proxy=" + proxy + "]";
    }
}
